package previous.Test_0201;

/**
 * Created with Intellij IDEA
 * Description:
 * User: 星辰
 */
public class Solution {
    //替换所有的问号
    //不包含任何 连续重复 的字符。
    //思路，
    // 1.遍历字符串
//    如果遇到问号，替换字符串
//    判断字符？前后的字符，要不与字符前后的字符不同
    public String modifyString(String s) {
          char[] ch=s.toCharArray();
          int len=ch.length;
        for (int i = 0; i < len; i++) {
            if(ch[i]=='?'){
                for (char j = 'a'; j < 'z'; j++) {
                    if((i==0|| ch[i-1]!=j) && (i==len-1 || ch[i+1]!=j)){
                        ch[i]=j;
                        break;
                    }
                }
            }
        }
        return String.valueOf(ch);
    }
    //提莫攻击
    //timeSeries = [1,4,5,9,13], duration = 2
    //如果x>=d  t+2;
    //如果x<d  t+x;
    public int findPoisonedDuration(int[] t, int d) {
        int len=t.length;
        int time=0;
        for (int right=1,left=0; right < len; right++,left++) {
            int x=t[right]-t[left];
            if(x>=d){
                time+=d;
            }else{
                time+=x;
            }
        }
        return time+d;
    }
    //z字形变换
    public String convert(String s, int n) {
          char[] ss=s.toCharArray();
          int len=ss.length;
         StringBuilder c=new StringBuilder();
          int d=2*n-2;
          if(n==1){
              return s;
          }
          //第一行
        for (int i = 0; i < len; i+=d) {
            c.append(ss[i]);
        }
        //中间行
        for (int k = 1; k <n-1 ; k++) {
            for (int i = k,j=d-i; i < len || j<len; i+=d,j+=d) {
                if(i<len) c.append(ss[i]);
                if(j<len) c.append(ss[j]);
            }
        }

        //最后一行
        for (int i = n-1; i < len; i+=d) {
            c.append(ss[i]);
        }
        return c.toString();
    }
    //外观数列
    public String countAndSay(int n) {
        String s="1";
        int len=s.length();
        int k=0;
        while(k++<n-1) {
            StringBuilder tmp=new StringBuilder();
            len=s.length();
            char[] ss=s.toCharArray();
            for (int left = 0, right = 0; left < len && right < len;) {
                while( right<len && ss[left]==ss[right] ){
                    right++;
                }
                tmp.append(right-left);
                tmp.append(ss[left]);
                left=right;
        }
        s=tmp.toString();
        }
        return s;
    }

}
